package com.info3k.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.commands.CommandHandler;
import com.info3k.client.components.Loader;
import com.info3k.client.components.LoginManager;
import com.info3k.client.components.MenuManager;
import com.info3k.client.components.Search;
import com.info3k.client.components.TabManager;
import com.info3k.client.messages.Info3KMessages;

public class Info3K implements EntryPoint {
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	public static Info3KServiceAsync infoService = GWT
			.create(Info3KService.class);
	
	public static Info3KMessages msgs = GWT
			.create(Info3KMessages.class);
	
	public static Loader loader = new Loader();
	 
	private LoginManager loginManager;
	private MenuManager menuManager;
	private TabManager tabManager;
	
	private Search search = new Search(this);

	public void onModuleLoad() {
		final HTML htmlTitle = new HTML("<span class=\"big\">" + msgs.info3KTitle().charAt(0) + "</span>" + msgs.info3KTitle().substring(1));
		
		htmlTitle.getElement().getStyle().setProperty("cursor", "pointer");
		
		htmlTitle.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				Window.open("/Info3K.html", "_self", "");
			}
		});
		
		final HTML htmlSlogan = new HTML(msgs.info3KSlogan());
		
		RootPanel.get("site_logo").add(htmlTitle);
		RootPanel.get("site_slogan").add(htmlSlogan);
		
		final HTML htmlLanguage = new HTML(msgs.chooseLanguage() + ": <a href=\"?locale=ba\">BA</a> || <a href=\"?locale=en\">EN</a>");

		loginManager = new LoginManager(this);
		menuManager = new MenuManager(this);
		
		VerticalPanel headerRight = new VerticalPanel();
		
		RootPanel.get("header_right").add(headerRight);
		
		headerRight.add(loginManager);
		headerRight.add(menuManager);
		
		tabManager = new TabManager(headerRight, this);		
		
		RootPanel.get("site_language").add(htmlLanguage);
		RootPanel.get().add(loader);
		
		RootPanel.get("search_section").add(search);
		
		new CommandHandler("latestEvents", this).execute();
	}

	public void setLoginManager(LoginManager loginManager) {
		this.loginManager = loginManager;
	}

	public LoginManager getLoginManager() {
		return loginManager;
	}

	public void setMenuManager(MenuManager menuManager) {
		this.menuManager = menuManager;
	}

	public MenuManager getMenuManager() {
		return menuManager;
	}

	public void setTabManager(TabManager tabManager) {
		this.tabManager = tabManager;
	}

	public TabManager getTabManager() {
		return tabManager;
	}
	
	public Search getSearch() {
		return search;
	}

	public void setSearch(Search search) {
		this.search = search;
	}
}
